#!/bin/bash
# 简单的MySQL复制监控脚本

MYSQL_USER="repuser"
MYSQL_PASS="WWW.1.com"

# 检查复制状态
repl_status=$(mysql -u${MYSQL_USER} -p${MYSQL_PASS} -e "SHOW SLAVE STATUS\G" 2> /dev/null | grep -E "Slave_IO_Running:|Slave_SQL_Running:|Seconds_Behind_Master:" ) 

io_running=$(echo "$repl_status" | grep "Slave_IO_Running:" | awk '{print $2}')
sql_running=$(echo "$repl_status" | grep "Slave_SQL_Running:" | awk '{print $2}')
delay=$(echo "$repl_status" | grep "Seconds_Behind_Master:" | awk '{print $2}')

# 输出连接信息和延迟
echo $io_running
echo $sql_running
echo $delay

#检查连接状态
if [ "$io_running" == "Yes" ] && [ "$sql_running" == "Yes" ]; then
    echo "IO与SQL进程均正常运行"
elif [ "$io_running" == "Yes" ] && [ "$sql_running" != "Yes" ]; then
    echo "IO进程运行正常，SQL进程异常"
elif [ "$io_running" != "Yes" ] && [ "$sql_running" == "Yes" ]; then
    echo "IO进程异常，SQL进程运行正常"
else
    echo "IO与SQL进程均未运行"
fi
# 检查复制延迟
if  [ $delay -eq 0 ]; then
    echo "复制延迟过高：${delay}秒" " $ALERT_EMAIL"
fi

